/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.Editors
{
	internal sealed partial class SandcastleBuildComponentBasicEditorDialog : Form
	{
		#region Public Properties
		public string Description
		{
			get
			{
				return descriptionTextBox.Text;
			}
			set
			{
				descriptionTextBox.Text = value;
			}
		}

		public string Configuration
		{
			get
			{
				return configurationTextBox.Text;
			}
			set
			{
				configurationTextBox.Text = value;
			}
		}
		#endregion

		#region Private / Protected
		private readonly bool errorMode;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="SandcastleBuildComponentBasicEditorDialog" /> class.
		/// </summary>
		public SandcastleBuildComponentBasicEditorDialog(SandcastleBuildComponent buildComponent)
		{
			if (buildComponent == null)
				throw new ArgumentNullException("buildComponent");
			
			// execute the code generated at design-time
			InitializeComponent();

			Exception ex = buildComponent.LoadException;

			if (ex == null)
			{
				descriptionTextBox.Text = buildComponent.CommentDescription;

				XmlDocument document = new XmlDocument();
				document.PreserveWhitespace = true;
				document.LoadXml(buildComponent.ConfigurationAsString);

				XmlElement root = document.DocumentElement;

				if (root != null)
				{
					System.Xml.XPath.XPathNavigator navigator = root.CreateNavigator();

					if (navigator.MoveToAttribute("type", ""))
						navigator.DeleteSelf();

					if (navigator.MoveToAttribute("assembly", ""))
						navigator.DeleteSelf();
				}

				configurationTextBox.Text = document.OuterXml;
			}
			else
			{
				errorMode = true;

				descriptionTextBox.ReadOnly = true;
				configurationTextBox.ReadOnly = true;

				descriptionTextBox.Text = ex.Message;
				configurationTextBox.Text = ex.ToString();

				descriptionLabel.Text = Resources.Text.LoadErrorMessage;
				configurationLabel.Text = Resources.Text.LoadError;

				cancelButton.Visible = false;

				okButton.Location = cancelButton.Location;
				okButton.DialogResult = DialogResult.Cancel;
			}

			Text = buildComponent.Name;

			positionLabel.Text = string.Format(System.Globalization.CultureInfo.CurrentCulture, 
				positionLabel.Text, buildComponent.Index + 1);
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="SandcastleBuildComponentBasicEditorDialog" /> class.
		/// </summary>
		private SandcastleBuildComponentBasicEditorDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		private void ValidateConfiguration()
		{
			string config = Configuration;

			if (!string.IsNullOrEmpty(config))
			{
				XmlDocument document = new XmlDocument();
				document.AppendChild(document.CreateElement("root")).InnerXml = config;
			}
		}
		#endregion

		#region Event Handlers
		private void configurationTextBox_Validating(object sender, CancelEventArgs e)
		{
			if (errorMode)
				return;

			try
			{
				ValidateConfiguration();

				errors.SetError(okButton, null);
			}
			catch (XmlException ex)
			{
				errors.SetError(okButton, string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Resources.Text.InvalidConfigurationXmlError, Environment.NewLine, ex.Message));

				int errorLine = ex.LineNumber;

				if (errorLine > 0 && errorLine <= configurationTextBox.Lines.Length)
				{
					if (errorLine == 1)
						configurationTextBox.SelectionStart = 0;
					else
					{
						int c = 0, n = 1;

						foreach (string line in configurationTextBox.Lines)
						{
							if (n++ == errorLine)
								break;
							else
								c += line.Length + Environment.NewLine.Length;
						}

						configurationTextBox.SelectionStart = c;
					}

					if (ex.LinePosition > 0)
						configurationTextBox.SelectionStart += ex.LinePosition - 1;

					configurationTextBox.SelectionLength = 1;
				}

				e.Cancel = true;
			}
		}
		#endregion
	}
}